package main

import (
	"fmt"

	"net/http"

	"github.com/gorilla/mux"
)

func main() {

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello: %s\n", r.URL.Path)
	})

	fs := http.FileServer(http.Dir("static/"))

	http.Handle("/static/", http.StripPrefix("/static/", fs))

	r := mux.NewRouter()
	r.HandleFunc("/books/{title}/page/{page}", func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		title := vars["title"]
		page := vars["page"]

		fmt.Fprintf(w, "You've requested the book: %s on page %s\n", title, page)
	}).Schemes("http")

	CreateBook := func(w http.ResponseWriter, r *http.Request) {
		resStr := "Post request"
		res := []byte(resStr)
		w.Write(res)
	}
	r.HandleFunc("/books/{title}", CreateBook).Methods("POST")

	ReadBook := func(w http.ResponseWriter, r *http.Request) {
		resStr := "Get request"
		res := []byte(resStr)
		w.Write(res)
	}
	r.HandleFunc("/books/{title}", ReadBook).Methods("GET")

	UpdateBook := func(w http.ResponseWriter, r *http.Request) {
		resStr := "Put request"
		res := []byte(resStr)
		w.Write(res)
	}
	r.HandleFunc("/books/{title}", UpdateBook).Methods("PUT")

	DeleteBook := func(w http.ResponseWriter, r *http.Request) {
		resStr := "Delete request"
		res := []byte(resStr)
		w.Write(res)
	}
	r.HandleFunc("/books/{title}", DeleteBook).Methods("DELETE")

	r.HandleFunc("/books/{title}", func(w http.ResponseWriter, r *http.Request) {}).Host("www.mybookstore.com")

	r.HandleFunc("/books/{title}", func(w http.ResponseWriter, r *http.Request) {}).Schemes("https")

	r.HandleFunc("/books/{title}", func(w http.ResponseWriter, r *http.Request) {}).Schemes("http")

	//sub router
	bookrouter := r.PathPrefix("/book").Subrouter()
	bookrouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		responStr := "request all books"
		res := []byte(responStr)

		w.Write(res)
	})

	bookrouter.HandleFunc("/{title}", func(w http.ResponseWriter, r *http.Request) {
		pathVars := mux.Vars(r)
		responStr := "get books " + pathVars["title"]
		res := []byte(responStr)

		w.Write(res)
	})

	http.ListenAndServe(":80", r)
}
